package com.jule.miaozhuapc.dao.impl;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import com.jule.miaozhuapc.dao.UserDao;
import com.jule.miaozhuapc.domain.WawaUser;
import com.jule.miaozhuapc.req.dto.WxUserInfo;

@Repository
public class UserDAOImpl implements UserDao{

	private EntityManager em;
	@PersistenceContext
	public void setEm(EntityManager em) {
		this.em = em;
	}
	
	@Override
	public WawaUser save(WawaUser bean) {
		em.persist(bean);
		return bean;
	}

	@Override
	@Query(name="WawaUser.findAll", value="SELECT w FROM WawaUser w")
	public WawaUser findByUsername() {
		List<WawaUser> userList  = em.createNamedQuery("WawaUser.findAll").getResultList();
		return userList.get(2);
	}
//		// TODO Auto-generated method stub
////		String hql = "from WawaUser where wxNickName=:wxNickName";
////		List<WawaUser> userList = em.createQuery(hql).setParameter("wxNickName", username).getResultList();
//		String hql = "FROM WawaUser ";
//		List<WawaUser> userList = em.createQuery(hql).getResultList();
//		if(userList!=null&&userList.size()>0)
//			return userList.get(0);
//		return null;
	//}


//	/**
//	 *  通过微信注册添加
//	 */
//	@Override
//	public WawaUser inserth5(WxUserInfo wxUser) {
//		// TODO Auto-generated method stub
//		String openId = wxUser.getOpenid();
//		String wxNickName = wxUser.getNickName();
//		String wxPic = wxUser.getHeadimgurl();
//		String wxGender = wxUser.getSex() == 1? "男" : "女";
//		if (openId == null) {
//			 // "参数缺失 注册失败";
//			return  null;
//		}
//		String hql = " from WawaUser where openId = ? and isDelete = 0";
//		WawaUser user = findByWxOpenId(openId);
//		//em.createQuery(hql, WawaUser.class).setParameter(0, openId).getSingleResult();
//		if (user == null) {
//			// 创建一个新的账号
////			String isql = "insert into WawaUser";
//			WawaUser wawauser = new WawaUser();
////			wawauser.setWxGender(wxGender);
////			wawauser.setWxNickName(wxNickName);
////			wawauser.setWxPic(wxPic);
////			wawauser.setWxOpenId(openId);
//			 em.persist(wawauser);
//		}
//		user  = em.find(WawaUser.class, openId);
//		System.out.println(" user" + user);
//		return user;
//	}
	
	@Override
	public WawaUser findByUserId(Long userId) {

		String hql = "from WawaUser where id=:userId and isDelete = 0";
		WawaUser wawaUser = em.find(WawaUser.class, userId);
		return wawaUser;
	}


	@Override
	public WawaUser findByWxOpenId(String wxOpenId) {

		String hql = "from WawaUser where wxOpenId=:wxOpenId and isDelete = 0";
		List<WawaUser>  wawaUsers = em.createQuery(hql, WawaUser.class)
				.setParameter("wxOpenId", wxOpenId)
				.getResultList();
		if(wawaUsers != null && wawaUsers.size() > 0){
			return  wawaUsers.get(0);
		}
		return null;
	}

	@Override
	public void updateUser(WawaUser wawaUser) {
		em.persist(wawaUser);
	}

	@Override
	public WawaUser findByToken(String token) {
		String hql = "from WawaUser where token=:token and isDelete = 0";
		List<WawaUser>  wawaUsers = em.createQuery(hql, WawaUser.class)
				.setParameter("token", token)
				.getResultList();
		if(wawaUsers != null && wawaUsers.size() > 0){
			return  wawaUsers.get(0);
		}
		return null;
	}
	
}
